<?php
    if(!$this->session->userdata('logged_in')) {
        redirect('logout');
    } else if($this->session->userdata('role') !== 'admin') {
        redirect('forbidden');
    }
?>
<div id="create_group_page">
    <div id="create_group_wrapper">
        <p id="p_create_group">Create a Group</p>
        <?php echo "<div id='error_message_area'>".$error_message."</div>"; ?>
        <?php echo form_open("groups/create", array('id' => 'create_group_form')); ?>
            <div class="note"> ALL fields are required. </div>
            <label for="groupName" id="lblGroupName">Group Name</label>
            <input type="text" name="groupName" id="create_group_name" value="<?php echo $group_name_temp; ?>"/>

            <label for="groupDesc" id="lblGroupDesc">Group Description</label>
            <textarea name="groupDesc" id="create_group_desc"><?php echo $group_desc_temp; ?></textarea>
            
            <p id="p_allow_subscribable">
            <?php
                if($isSubs == true) {
                    echo "<input type='checkbox' name='isSubs' value='make_subscribable' id='create_group_subscribe' onchange='give_password()' checked/>";
                }
                if($isSubs == false) {
                    echo "<input type='checkbox' name='isSubs' value='make_subscribable' id='create_group_subscribe' onchange='give_password()' />";
                }
            ?>
            Allow all registered users to subscribe to this group</p>
            
            <label for="group_password" id="lbl_groupPassword">Group Password <b>(optional)</b></label>
            <input type="text" name="group_password" id="group_password" disabled />
            
            <div id="available_admins_area">
            <p id="admins_header">Group Admins</p>
            <p id="p_choosePeople">Choose people who can post in this group: <strong>(at least one</strong>)</p>
            <p id="p_find">Display
                <select id="display_method_select" name="display_method_select">
                    <option value="All">Admins and Authors</option>
                    <option value="Authors">Authors Only</option>
                    <option value="Admins">Site Admins only</option>
                </select>
                <button id="btn_go" name="btn_go">Go</button>
            </p>
            
            <?php
            for($i = 0; $i < count($available_emps); $i++) {
              $id = $available_emps[$i]['user_id'];
              $name = $available_emps[$i]['fname'].' '.$available_emps[$i]['lname'];
              
              if(in_array($id, $admins)) {
                  echo "<p><input type='checkbox' name='available_emps[]' class='available_inst' value=$id checked='checked' /> $name</p>";
              }
              else {
                 echo "<p><input type='checkbox' name='available_emps[]' class='available_inst' value=$id/> $name</p>";
              }
            }
            
           ?>
            
            <input type="submit" name="submit1" value="Create Group" id="create_group_save" />
            <input type="button" id="btn_cancel" value="Cancel" onclick="window.location='<?php echo base_url() ?>groups';" />
        <?php echo form_close(); ?>
             
    </div>
</div>
  </div>

<script>
    var pp_check = document.getElementsByName('pp_check');
    var pp_checkall = document.getElementsByName('pp_checkall')[0];
    
    function checkAll() {
            if (pp_checkall.checked == true) {
                for (i = 0; i < pp_check.length; i++)
                    pp_check[i].checked = true;
            }
            else {
                for (i = 0; i < pp_check.length; i++)
                    pp_check[i].checked = false;
            }
    }
    
    
    function changeList() {
        var selectList = document.getElementById('display_method_select');
        var selected = selectList.options[selectList.selectedIndex].value;
        
        var parentDiv = document.getElementById('available_admins_area');
        
        var listOfEmps = document.getElementsByClassName('available_inst');
        var labels = document.getElementsByClassName('lbl_chklabel');
        var numInList = <?php echo count($available_emps); ?>;
        
        //for(var i=0; i < numInList; i=0) {
        //    listOfEmps[i].parentNode.removeChild(listOfEmps[i]);
        //    labels[i].parentNode.removeChild(labels[i]);
        //}
        
        
        /*if(selected === 'All') {
            for(var i=0; i < <?php echo count($available_emps); ?>; i++) {
                var id = <?php echo $available_emps; ?>[i]<?php echo ['user_id']; ?>;
                var name = <?php echo $available_emps; ?>[i]<?php echo ['fname']; ?> <?php echo $available_emps; ?>[i]<?php echo ['lname']; ?>;
                
                var pContainer = document.createElement("p");
                var newCheckbox = document.createElement("input");
                newCheckbox.type = 'checkbox';
                newCheckbox.id = 'checkbox1';
                newCheckbox.className = 'available_inst';
                newCheckbox.name = 'available_emps[]';
                newCheckbox.value = id;
                
                var newLabel = document.createElement('label');
                newLabel.setAttribute('for', 'checkbox1');
                newLabel.className = 'lbl_chklabel';
                newLabel.appendChild(document.createTextNode(name));
            }
        }*/
        
    }
    
    function give_password() {
        
        var passwordField = document.getElementById('group_password');
        var checkbox = document.getElementById('create_group_subscribe');
        
        if(checkbox.checked === true) {
            passwordField.disabled = false;
        }
        else if(checkbox.checked === false) {
            passwordField.disabled = true;
        }
    }
    
</script>